﻿using MediatR;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.EndingOnly
{
    public class ProductionScratchesHTmlHandler : IRequestHandler<ProductionScratchesHTmlCommondcs, PageDTO<ProductionScratches>>
    {

        private readonly IBateRepository<ProductionScratches> _ProductionScratches;

        public ProductionScratchesHTmlHandler(IBateRepository<ProductionScratches> productionScratches)
        {
            _ProductionScratches = productionScratches;
        }

        public async Task<PageDTO<ProductionScratches>> Handle(ProductionScratchesHTmlCommondcs request, CancellationToken cancellationToken)
        {


            try
            {
                var list = new PageDTO<ProductionScratches>() { pageCount = 0, Lists = new List<ProductionScratches>(), totalCount = 0 };




                var query = _ProductionScratches.GetAll().ToList();


                list.totalCount = query.Count;
                list.pageCount = (int)Math.Ceiling(query.Count * 1.0 / request.PageSize);


                var num = (request.Pageindex - 1) / request.PageSize;


                query = query.OrderBy(x => x.ProductionScratchesId).Skip(num).Take(request.PageSize).ToList();



                list.Lists = query.ToList();


                return list;
            }
            catch (Exception)
            {

                throw;
            }
            

        }
    }
}
